package com.junhong.util;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class CommonUtil {

	/**
	 * get classloader
	 * 
	 * @return
	 */
	public static final ClassLoader getThreadLoader() {

		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		if (cl == null) {
			cl = ClassLoader.getSystemClassLoader();
		}
		return cl;
	}

	/**
	 * URLConnection can not be used to write a file
	 * 
	 * @param fileName
	 * @param content
	 */
	public static final void writeToFile(String fileName, String content) {
		OutputStream ops = null;
		try {
			URL fileURL = ClassLoader.getSystemResource("config/" + fileName);
			URLConnection conn = fileURL.openConnection();
			conn.setDoOutput(true);
			ops = conn.getOutputStream();
			ops.write(content.getBytes());
		} catch (IOException ioEx) {
			ioEx.printStackTrace();
		} finally {
			try {
				if (ops != null) {
					ops.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

	public static void main(String[] args) {

		writeToFile("clientIPAddress.log", "test");

	}
}
